<?php

class MY_Controller extends CI_Controller {

	var $data = array();

	public function __construct()
	{
		parent::__construct();
		
		if( !$this->check_session_is_valid() )
		{
			redirect('/SignIn');
			return;
		}
		
		//send the style and title to the menu
		$this->data = array( 'style'=>'classic_style.css', 'title'=>'Then I Tried It...', 'user'=>'username');
		
		//send the user data to each page
		$sessionData = $this->session->all_userdata();
		$userID = $sessionData['userID'];
		$userObj = $this->user->get_user($userID);
		$this->data['user'] = $userObj;
		
		$this->data['menu'] = $this->load->view('user_menu', $this->data, true) . $this->load->view('moderator_menu', $this->data, true) . $this->load->view('admin_menu', $this->data, true);
	}
	
	private function check_session_is_valid()
	{
		$this->load->model('User_model', 'user');
		$sessionData = $this->session->all_userdata();
		if( !isset( $sessionData['userID'] ) )
		{
			return FALSE;
		}
		$userID = $sessionData['userID'];
		$userObj = $this->user->get_user($userID);
		
		if( $userObj['sessionID'] != $sessionData['my_session_id'] )
		{
			return FALSE;
		}
		
		return TRUE;
	}
}
?>